﻿using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Expression.Interactivity.Core;
using QDP.Platform.ObjService;
using QDP.Platform.Silverlight.Controls;
namespace QDP.Platform.Silverlight
{
    /// <summary>表示一个组件模型对象的实例。</summary>
    public class Widget : ObservableObject
    {
        const string Property_Navigation = "Navigation";
        const string Property_Children = "Children";
        const string Property_PartElement = "PartElement";
        const string Property_TileTemplate = "TileTemplate";
        const string Property_Configs = "Configs";

        private NavigationBase _Navigation;
        private PartElement _PartElement;
        private ControlTemplate _TileTemplate;
        private Dictionary<string, string> _Configs;
        private List<Widget> _Children;
        /// <summary>以默认参数构造一个组件模型对象的实例。</summary>
        public Widget()
        {
            this.Configs = new Dictionary<string, string>();
            this.Children = new List<Widget>();
            this.Command = new ActionCommand(this.OnCommand);
        }
        /// <summary>获取或设置导航标签信息对象。</summary>
        public NavigationBase Navigation
        {
            get { return _Navigation; }
            set
            {
                if (_Navigation != value)
                {
                    _Navigation = value;
                    Notify(Property_Navigation);
                }
            }
        }
        /// <summary>获取或设置组件对象包含的子对象清单。</summary>
        public List<Widget> Children
        {
            get { return _Children; }
            set
            {
                if (_Children != value)
                {
                    _Children = value;
                    Notify(Property_Children);
                }
            }
        }
        /// <summary>获取或设置组件对象的示例。</summary>
        public PartElement PartElement
        {
            get { return _PartElement; }
            set
            {
                if (_PartElement != value)
                {
                    _PartElement = value;
                    Notify(Property_PartElement);
                }
            }
        }
        /// <summary>获取或设置组件对象的类别。</summary>
        internal WidgetType Type { get; set; }
        /// <summary>获取或设置组件对象的磁贴模板，若没有则使用默认值。</summary>
        public ControlTemplate TileTemplate
        {
            get { return _TileTemplate; }
            set
            {
                if (_TileTemplate != value)
                {
                    _TileTemplate = value;
                    Notify(Property_TileTemplate);
                }
            }
        }
        /// <summary>获取或设置组件对象的配置信息。</summary>
        public Dictionary<string, string> Configs
        {
            get { return _Configs; }
            set
            {
                if (_Configs != value)
                {
                    _Configs = value;
                    Notify(Property_Configs);
                }
            }
        }
        /// <summary>获取一个值，该值指定组件对象是否含有子对象。</summary>
        public bool HasChildren { get { return Children.Count != 0; } }

        public ICommand Command { get; private set; }

        private void OnCommand()
        {
            Q.WidgetManager.GetWidget(this.Navigation.Code, LoadAction);
        }
        private void LoadAction(LoadActionArgs ActionArgs)
        {
            switch (ActionArgs.State)
            {
                case LoadActionState.Error:
                    {
#if DEBUG
                        this.ShowMessage(ActionArgs.Error.Message,ActionArgs.Error.ToString());
#else
                        this.Show("应用程序无法加载，请与维护人员联系！");
#endif
                        break;
                    }
                case LoadActionState.Complated:
                    {
                        var part = ActionArgs.Element;
                        // 判断组件是否已经展开，若为true则调用关闭方法，否则调用打开方法。
                        if (part.IsShowed)
                        {
                            string TileAction = part.GetConfigValue("Action");
                            // 当应用处于未激活状态时，点击磁贴所执行的操作：默认为关闭，Active为激活；
                            if (TileAction.Equals("Active") && part.IsActived == false)
                            {
                                part.Active();
                            }
                            else
                            {
                                if (part.WidgetContent is WidgetControl)
                                {
                                    var ctrl = part.WidgetContent as WidgetControl;
                                    if (ctrl.IsMinimized)
                                        ctrl.OnRestore();
                                    else
                                        ctrl.OnClose();
                                }
                                else
                                {
                                    part.Close();
                                }
                            }
                        }
                        else
                        {
                            part.Show();
                        }
                        break;
                    }
            }
        }
    }
}